home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / mosmllib / String.sml < prev    next >
Encoding:
Text File  |  1996-07-03  |  3.3 KB  |  115 lines  |  [TEXT/R*ch]

  1. (* String -- 1994-12-10 *)
  2.  
  3. local 
  4.     type char = Char.char;
  5.     prim_val sub_      : string -> int -> char         = 2 "get_nth_char";
  6.     prim_val mkstring_ : int -> string                 = 1 "create_string";
  7.     prim_val update_   : string -> int -> char -> unit = 3 "set_nth_char";
  8.     prim_val strcmp_   : string -> string -> int       = 2 "compare_strings";
  9.     prim_val blit_     : string -> int -> string -> int -> int -> unit 
  10.                                                        = 5 "blit_string";
  11.     fun (f o g) x = f (g x);
  12. in
  13.  
  14. type string = string
  15. val maxLen = Strbase.maxlen
  16. val size = size
  17.  
  18. fun sub(s, i) = 
  19.     if i<0 orelse i >= size s then raise Subscript
  20.     else sub_ s i;
  21.  
  22. fun concat strs =
  23.     let fun acc [] len       = len
  24.           | acc (v1::vr) len = acc vr (size v1 + len)
  25.         val len = acc strs 0
  26.         val newstr = if len > maxLen then raise Size else mkstring_ len 
  27.         fun copyall to []       = ()
  28.           | copyall to (v1::vr) = 
  29.             let val len1 = size v1
  30.             in blit_ v1 0 newstr to len1; copyall (to+len1) vr end
  31.     in copyall 0 strs; newstr end;
  32.  
  33. val op ^ = op ^;
  34.  
  35. fun str c = 
  36.     let val newstr = mkstring_ 1
  37.     in update_ newstr 0 c; newstr end;
  38.  
  39. fun implode cs = 
  40.   let val n = List.length cs
  41.       val newstr = if n > maxLen then raise Size else mkstring_ n
  42.       fun init []      i = ()
  43.         | init (c::cr) i = (update_ newstr i c; init cr (i+1))
  44.   in (init cs 0; newstr) end;
  45.  
  46. fun extract (s, i, slicelen) =
  47.     let val n = case slicelen of NONE => size s - i | SOME n => n
  48.     val newstr = if i<0 orelse n<0 orelse n>size s-i then raise Subscript
  49.              else mkstring_ n
  50.     in blit_ s i newstr 0 n; newstr end;
  51.  
  52. fun substring (s, i, n) = extract(s, i, SOME n);
  53.  
  54. fun explode s =
  55.     let fun h j res = if j<0 then res
  56.               else h (j-1) (sub_ s j :: res)
  57.     in h (size s - 1) [] end;
  58.  
  59. fun compare (s1, s2) = 
  60.     (case strcmp_ s1 s2 of
  61.          ~2 => LESS
  62.        | ~1 => LESS
  63.        |  0 => EQUAL
  64.        |  1 => GREATER
  65.        |  2 => GREATER
  66.        |  _ => raise Fail "internal error: String.compare");
  67.  
  68. fun collate cmp (s1, s2) =
  69.     let val n1 = size s1 
  70.     and n2 = size s2
  71.     val stop = if n1 < n2 then n1 else n2
  72.     fun h j = (* At this point s1[0..j-1] = s2[0..j-1] *)
  73.         if j = stop then if      n1 < n2 then LESS
  74.                              else if n1 > n2 then GREATER
  75.                              else                 EQUAL
  76.         else
  77.         case cmp(sub_ s1 j, sub_ s2 j) of
  78.             LESS    => LESS
  79.           | GREATER => GREATER
  80.           | EQUAL   => h (j+1)
  81.     in h 0 end;
  82.  
  83. fun translate f s = 
  84.     Strbase.translate f (s, 0, size s);
  85.  
  86. fun tokens p s = 
  87.     List.map substring (Strbase.tokens p (s, 0, size s));
  88.  
  89. fun fields p s = 
  90.     List.map substring (Strbase.fields p (s, 0, size s));
  91.  
  92. fun foldl f e s = 
  93.     let val stop = size s
  94.     fun h j res = if j>=stop then res 
  95.               else h (j+1) (f (sub_ s j, res))
  96.     in h 0 e end;
  97.  
  98. fun foldr f e s = 
  99.     let fun h j res = if j<0 then res 
  100.                       else h (j-1) (f (sub_ s j, res))
  101.     in h (size s - 1) e end;
  102.  
  103. fun find p s = 
  104.     let val stop = size s
  105.     fun h j = if j>=stop then NONE 
  106.           else if p (sub_ s j) then SOME j
  107.           else h (j+1) 
  108.     in h 0 end;
  109.  
  110. val op <  = op <  : string * string -> bool;
  111. val op <= = op <= : string * string -> bool;
  112. val op >  = op >  : string * string -> bool;
  113. val op >= = op >= : string * string -> bool;
  114. end
  115.